local CommonOHOH = _G.CommonOHOH;

local IsClient = game:GetService("RunService"):IsClient();
local M = CommonOHOH.class.new(script, CommonOHOH.files.BaseObject());
local ObjectManager = CommonOHOH.load.ObjectManager();


function M.new(...)
	return M(...);
end;

function M.ctor(self)
    M.super.ctor(self);
    self.__components = {};
    self.__privateComponent={}
    if IsClient then
        self:ClientInit()
    else
        self:ServerInit()
    end
end;

function M.dtor(self)
    for v2, v3 in pairs(self.__components) do
		coroutine.resume((coroutine.create(function()
			local v4, v5 = xpcall(v3.dtor, debug.traceback, v3);
			if not v4 then
				v3[v2] = nil;
				ObjectManager:DelObject(v3);
			end;
		end)));
	end;
	while next(self.__components) and os.clock() - os.clock() <= 10 do
		wait();	
	end;

	M.super.dtor(self);
end;

function M.ServerInit(self)


end

function M.ClientInit(self)


end


function M.AddComponent(self, component,...)
	return self.__components[component.class] or component.new(self,...);
end;
function M.GetComponent(self, component)
	return self.__components[component.class];
end;

function M.AddComponent_Common_Client_Server(self,common,client,server)
    if common then
        self:AddPrivateComponentByFolder(common)
    end
    if IsClient and client then
        self:AddPrivateComponentByFolder(client)
    end

    if not IsClient and server then
        self:AddPrivateComponentByFolder(server)
    end
end

function M.AddPrivateComponentByFolder(self,folder)
    for _,component in pairs(folder:GetChildren()) do
       self:AddPrivateComponent(component)
    end
end
function M.AddPrivateComponent(self,scriptText)
    self.__privateComponent[scriptText.Name] =self:AddComponent(require(scriptText))
    return  self.__privateComponent[scriptText.Name]
end
function M.GetPrivateComponent(self,calssName)
    return self.__privateComponent[calssName]
end



return M
